/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converter.view;

import converter.controller.ConverterFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import converter.model.Currency;
import java.util.List;
import java.util.Hashtable;
import java.util.Iterator;

/**
 *
 * @author reverende
 */
@Named(value = "currencyManager")
@ConversationScoped //use request scoped instead?
public class CurrencyManager implements Serializable {
    @EJB
    private ConverterFacade converterFacade;
    private String convertFrom;
    private String convertTo;
    private double amountToConvert;

    public double getAmountToConvert() {
        return amountToConvert;
    }
    private String newCurrencyName;
    private double newCurrencyExchangeRate;
    private double valueInCurrency;
    private boolean conversionDone = false;

    public boolean isConversionDone() {
        return conversionDone;
    }

    public void setConversionDone(boolean conversionDone) {
        this.conversionDone = conversionDone;
    }
    
    @Inject
    private Conversation conversation;
    
    /**
     * Creates a new instance of CurrencyManager
     */
    public CurrencyManager() {
    }
    
      public String getConvertTo() {
        return convertTo;
    }

    public void setConvertTo(String convertTo) {
        this.convertTo = convertTo;
    }

    public double getValueInCurrency() {
        return valueInCurrency;
    }

    public void setValueInCurrency(double valueInCurrency) {
        this.valueInCurrency = valueInCurrency;
    }
    
    public String getConvertFrom() {
        return convertFrom;
    }

    public void setConvertFrom(String convertFrom) {
        this.convertFrom = convertFrom;
    }

    public String getNewCurrencyName() {
        return newCurrencyName;
    }

    public double getNewCurrencyExchangeRate() {
        return newCurrencyExchangeRate;
    }

    

    public void convert(){
        
        double exchangeRateFrom = converterFacade.findCurrency(convertFrom).getExchangeRate();
        double exchangeRateTo = converterFacade.findCurrency(convertTo).getExchangeRate();
        valueInCurrency = amountToConvert * (exchangeRateFrom/exchangeRateTo);
        conversionDone = true;
    }
    
    public void newCurrency(){
        converterFacade.createCurrency(newCurrencyName, newCurrencyExchangeRate);
    }
    
    public List<Currency> getCurrencies()
    {
        return converterFacade.getCurrencies();
    }
    
    public void setAmountToConvert(double amountToConvert) {
        this.amountToConvert = amountToConvert;
    }

    public void setNewCurrencyName(String newCurrencyName) {
        this.newCurrencyName = newCurrencyName;
    }

    public void setNewCurrencyExchangeRate(double newCurrencyExchangeRate) {
        this.newCurrencyExchangeRate = newCurrencyExchangeRate;
    }
}
